/**
 * IHIS 1.0 by HoneyBread (김민호, 이충희, 이성연, 신경민)
 * SEP 8기 KAIST
 */
package ihis.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

/**
 * @author minho kim
 *
 */
public class GeneralFilter implements Serializable{
	private static final long serialVersionUID = 2174693295556589894L;
	protected Date startDate;
	protected Date endDate;
	protected int periodType = -1;
	protected int ownerType = -1;
	protected String projectId;
//	protected String userId;
	protected String teamId;
	protected SortField sort;
	protected Boolean sortAscending;
	protected String ownerId;
	protected String reportDate;
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append("SDATE:").append(startDate).append(" ")
			.append("EDATE:").append(endDate).append(" ")
			.append("PTYPE:").append(periodType).append(" ")
			.append("OTYPE:").append(ownerType).append(" ")
			.append("PID:").append(projectId).append(" ")
			.append("TID:").append(teamId).append(" ")
			.append("OID:").append(ownerId).append(" ")
			.append("RDATE:").append(reportDate);
		return sb.toString();
	}
	
	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}
	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}
	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	/**
	 * @return the periodType
	 */
	public int getPeriodType() {
		return periodType;
	}
	/**
	 * @param periodType the periodType to set
	 */
	public void setPeriodType(int periodType) {
		this.periodType = periodType;
	}
	/**
	 * @return the projectId
	 */
	public String getProjectId() {
		return projectId;
	}
	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}
	/**
	 * @return the userId
	 */
	public String getUserId() {
		return ownerId;
	}
	/**
	 * @param userId the userId to set
	 */
	public void setUserId(String userId) {
		this.ownerId = userId;
	}
	/**
	 * @return the teamId
	 */
	public String getTeamId() {
		return teamId;
	}
	/**
	 * @param teamId the teamId to set
	 */
	public void setTeamId(String teamId) {
		this.teamId = teamId;
	}
	/**
	 * @return the ownerType
	 */
	public int getOwnerType() {
		return ownerType;
	}
	
	/**
	 * @param ownerType the ownerType to set
	 */
	public void setOwnerType(int ownerType) {
		this.ownerType = ownerType;
	}
	/**
	 * @return the sort
	 */
	public SortField getSort() {
		return sort;
	}
	/**
	 * @param sort the sort to set
	 */
	public void setSort(SortField sort) {
		this.sort = sort;
	}
	/**
	 * @return the sortAscending
	 */
	public Boolean getSortAscending() {
		return sortAscending;
	}
	/**
	 * @param sortAscending the sortAscending to set
	 */
	public void setSortAscending(Boolean sortAscending) {
		this.sortAscending = sortAscending;
	}
	/**
	 * @return the ownerId
	 */
	public String getOwnerId() {
		return ownerId;
	}
	/**
	 * @param ownerId the ownerId to set
	 */
	public void setOwnerId(String ownerId) {
		this.ownerId = ownerId;
	}
	/**
	 * @return the reportDate
	 */
	public String getReportDate() {
		return reportDate;
	}
	/**
	 * @param reportDate the reportDate to set
	 */
	public void setReportDate(String reportDate) {
		this.reportDate = reportDate;
	}
	
	
	public static void generateNextDate(ihis.model.TaskFilter filter, int unit)
	{
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.setTime(filter.getStartDate());
		// 주간이면 그 날을 포함한 월요일로, 월간이면 그월 1일, 년간이면 그해 1월 1일
		switch(unit)
		{
			case IHISConstants.TIME_UNIT_WEEKLY:
				cal.add(Calendar.DATE, 7);
				break;
			case IHISConstants.TIME_UNIT_MONTHLY:
				cal.add(Calendar.MONTH, 1);
				break;
			case IHISConstants.TIME_UNIT_YEARLY:
				cal.add(Calendar.YEAR, 1);
				break;
		}
		filter.setStartDate(cal.getTime());
		
		cal.setTime(filter.getEndDate());
		// 주간이면 그 날을 포함한 월요일로, 월간이면 그월 1일, 년간이면 그해 1월 1일
		switch(unit)
		{
			case IHISConstants.TIME_UNIT_WEEKLY:
				cal.add(Calendar.DATE, 7);
				break;
			case IHISConstants.TIME_UNIT_MONTHLY:
				cal.add(Calendar.MONTH, 1);
				break;
			case IHISConstants.TIME_UNIT_YEARLY:
				cal.add(Calendar.YEAR, 1);
				break;
		}
		filter.setEndDate(cal.getTime());
	}

	public static Date generateFromDate(Date inDate, int unit)
	{
		Calendar cal = Calendar.getInstance();

		cal.clear();
		cal.setTime(inDate);
		// 주간이면 그 날을 포함한 월요일로, 월간이면 그월 1일, 년간이면 그해 1월 1일
		switch(unit)
		{
			case IHISConstants.TIME_UNIT_WEEKLY:
				while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY)
				{
					cal.add(Calendar.DATE, -1);
				}
				break;
			case IHISConstants.TIME_UNIT_MONTHLY:
				if(cal.get(Calendar.DATE) != 1)
					cal.add(Calendar.DATE, 1 - cal.get(Calendar.DATE));
				break;
			case IHISConstants.TIME_UNIT_YEARLY:
				if(cal.get(Calendar.MONTH) != 0)
					cal.add(Calendar.MONTH, - cal.get(Calendar.MONTH));
				if(cal.get(Calendar.DATE) != 1)
					cal.add(Calendar.DATE, 1 - cal.get(Calendar.DATE));
				break;
		}
		cal.set(Calendar.HOUR,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND, 0);
		
		return cal.getTime();
	}
	
	public static Date generateToDate(Date inDate, int unit, boolean beforeNow)
	{
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.setTime(inDate);
		// 주간이면 다음 월요일로, 월간이면 다음월 1일, 년간이면 내년 1월 1일에서 1milsec 빼면 됨.
		switch(unit)
		{
			case IHISConstants.TIME_UNIT_WEEKLY:
				cal.add(Calendar.DATE, 1);
				while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY)
				{
					cal.add(Calendar.DATE, 1);
				}
				break;
			case IHISConstants.TIME_UNIT_MONTHLY:
				cal.add(Calendar.MONTH, 1);
				if(cal.get(Calendar.DATE) != 1)
					cal.add(Calendar.DATE, 1 - cal.get(Calendar.DATE));
				break;
			case IHISConstants.TIME_UNIT_YEARLY:
				cal.add(Calendar.YEAR, 1);
				if(cal.get(Calendar.MONTH) != 0)
					cal.add(Calendar.MONTH, - cal.get(Calendar.MONTH));
				if(cal.get(Calendar.DATE) != 1)
					cal.add(Calendar.DATE, 1 - cal.get(Calendar.DATE));
				break;
		}
		cal.add(Calendar.MILLISECOND, -1);
		long millis = System.currentTimeMillis();
		Date date;
		if(beforeNow && cal.getTimeInMillis() > millis)
			date = new Date(millis);
		else 
			date = cal.getTime();
		return date;
	}

}
